package des.wangku.operate.standard.dialog;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import des.wangku.operate.standard.swt.table.FormatStyle;
import des.wangku.operate.standard.utls.UtilsDialogState;

import static des.wangku.operate.standard.swt.table.FormatStyle.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.DateTime;

/**
 * 输入名称
 * 
 * @author Sunjian
 * @version 1.0
 * @since jdk1.8
 */
public class InputValueDialog extends Dialog {

	/** 日志 */
	static Logger logger = LoggerFactory.getLogger(InputValueDialog.class);

	protected Shell shell;

	String result = null;

	String oldValue = "";

	FormatStyle fs = TEXT;

	Text text = null;

	/**
	 * 构造函数 Composite.getShell()转成Shell
	 * 
	 * @param com Composite
	 * @param style int
	 * @param shellText String
	 * @param labelText String
	 * @param oldValue String
	 * @param readonly boolean
	 * @wbp.parser.constructor
	 */
	public InputValueDialog(Composite com, int style, String shellText, String labelText, String oldValue, boolean readonly) {
		this(com.getShell(), style, shellText, labelText, oldValue, readonly, TEXT);
	}

	/**
	 * 构造函数
	 * 
	 * @param parent Shell
	 * @param style int
	 * @param shellText String
	 * @param labelText String
	 * @param oldValue String
	 * @param readonly boolean
	 */
	public InputValueDialog(Shell parent, int style, String shellText, String labelText, String oldValue, boolean readonly, FormatStyle fs) {
		super(parent, style);
		this.parent = parent;
		this.shell = new Shell(parent);
		this.fs = fs;
		if (shellText != null) this.shellText = shellText;
		this.labelText = labelText;
		this.oldValue = oldValue;
		this.readonly = readonly;
		initialization();
	}

	public InputValueDialog(Shell parent, int style) {
		super(parent, style);
		this.shell = new Shell(parent);
		initialization();
	}

	Shell parent = null;

	/**
	 * 窗口文本
	 */
	String shellText = "";

	/**
	 * 文本标题
	 */
	String labelText = "标题";

	/**
	 * 是否只读
	 */
	boolean readonly = false;

	Button button = null;

	private void initialization() {
		shell.setSize(319, 165);
		UtilsDialogState.changeDialogCenter(parent, this.shell);
		Label label = new Label(shell, SWT.NONE);
		label.setAlignment(SWT.RIGHT);
		label.setBounds(0, 24, 39, 12);
		label.setText(labelText);
		int height = 18;
		int textstype = SWT.BORDER;
		if (isMulti()) {
			shell.setSize(286, 158);
			// button.setLocation(101, 100);
			textstype = SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP;
			height = 70;
		}
		text = new Text(shell, textstype);
		text.setBounds(53, 21, 222, height);
		text.setFocus();
		button = new Button(shell, SWT.NONE);
		button.setBounds(101, 108, 101, 22);
		button.setText("确定");
		if (fs == DATE || fs == TIME) {
			Utils.init_Date(shell, fs, text);
		}
	}

	/**
	 * 初邕化
	 */
	public void init() {
		String title = shellText;
		if (readonly) title = title + "(只读)";
		shell.setText(title);
		button.setVisible(!readonly);
		if (oldValue != null) text.setText(oldValue);
		button.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				result = text.getText().trim();
				shell.dispose();
			}
		});
		if (fs == FILENAMEMULTI || fs == PATHFILESINGLE || fs == PATHFILEMULTI|| fs==PATHCATAL) {
			switch (fs) {
				case PATHFILESINGLE:
					text.setToolTipText("双击选择单独文件");
					break;
				case PATHFILEMULTI:
					text.setToolTipText("双击选择多个文件");
					break;
				default:
					text.setToolTipText("双击选择多个文件");
			}
			textAddListener();
		}
	}

	private void textAddListener() {
		text.addMouseListener(new MouseListener(){

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if (fs == FILENAMEMULTI || fs == PATHFILESINGLE || fs == PATHFILEMULTI) {
					int style = fs == PATHFILESINGLE ? SWT.SINGLE : SWT.MULTI;
					FileDialog fileselect = new FileDialog(shell, style);
					switch (fs) {
						case PATHFILESINGLE:
							fileselect.setText("选择单独文件");
							break;
						case PATHFILEMULTI:
							fileselect.setText("选择多个文件");
							break;
						default:
							fileselect.setText("选择多个文件");
					}
					String url = fileselect.open();
					if (fs == PATHFILESINGLE) {
						if (url == null) return;
						text.setText(url);
						return;
					}
					String[] arrs = fileselect.getFileNames();
					String path = fileselect.getFilterPath() + "\\";
					StringBuilder sb = new StringBuilder();
					for (String f : arrs) {
						if (sb.length() > 0) sb.append(";");
						String line = f;
						if (fs == PATHFILEMULTI) line = path + f;
						sb.append(line);
					}
					text.setText(sb.toString());
				}
				if (fs == PATHCATAL) {
					DirectoryDialog dialog = new DirectoryDialog(shell,SWT.NONE);
					dialog.setFilterPath("c:\\");
					dialog.setMessage("只能选择唯一文件夹，返回的是文件夹路径");
					dialog.setText("选择文件夹");
					String url=dialog.open();
					if (url == null) {
						return;
					}
					text.setText(url);
					return;
				}
			}

			@Override
			public void mouseDown(MouseEvent e) {
			}

			@Override
			public void mouseUp(MouseEvent e) {
			}
		});
	}

	/**
	 * 判断是否含有换行，先判断是否长度大于20，如果大于20则多行显示
	 * 
	 * @return boolean
	 */
	boolean isMulti() {
		if (oldValue.length() > 20) return true;
		if (oldValue.indexOf('\n') != -1) return true;
		return false;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * 初始化一些控件
	 * 
	 * @author Sunjian
	 * @version 1.0
	 * @since jdk1.8
	 */
	public static final class Utils {

		public static void init_Date(Shell shell, FormatStyle fs, Text text) {
			DateTime date = new DateTime(shell, SWT.BORDER);
			date.setBounds(53, 45, 80, 23);
			DateTime time = new DateTime(shell, SWT.TIME);
			time.setBounds(130, 45, 80, 23);
			time.setEnabled(false);
			time.setVisible(false);
			if (fs == TIME) {
				time.setEnabled(true);
				time.setVisible(true);
			}
			init_Date_DateTimeListener(fs, date, date, time, text);
			if (fs == TIME) {
				init_Date_DateTimeListener(fs, time, date, time, text);
			}
		}

		public static void init_Date_Show(FormatStyle fs, DateTime date, DateTime time, Text text) {
			int year = date.getYear();
			int month = (date.getMonth() + 1);
			int day = date.getDay();
			String dateVal = year + "-" + month + "-" + day;
			String result = dateVal;
			if (fs == TIME) {
				int hour = time.getHours();
				int min = time.getMinutes();
				int sec = time.getSeconds();
				String timeVal = hour + ":" + min + ":" + sec;
				result += " " + timeVal;
			}
			text.setText(result);
		}

		public static void init_Date_DateTimeListener(FormatStyle fs, DateTime base, DateTime date, DateTime time, Text text) {
			base.addSelectionListener(new SelectionListener(){

				@Override
				public void widgetSelected(SelectionEvent e) {
					init_Date_Show(fs, date, time, text);
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
				}
			});
			base.addMouseListener(new MouseListener(){

				@Override
				public void mouseDoubleClick(MouseEvent e) {
				}

				@Override
				public void mouseDown(MouseEvent e) {
					init_Date_Show(fs, date, time, text);
				}

				@Override
				public void mouseUp(MouseEvent e) {
					init_Date_Show(fs, date, time, text);
				}
			});
		}
	}
}
